<template>
    <div class="layout-container">
        <!-- 侧边栏 -->
        <div class="sidebar">
            <div class="logo">
                <router-link to="/dashboard">智能家居系统</router-link>
            </div>
            <el-menu :default-active="activeMenu" class="sidebar-menu" background-color="#001529" text-color="#fff"
                active-text-color="#409EFF" @select="handleSelect">
                <el-menu-item index="/dashboard">
                    <el-icon>
                        <House />
                    </el-icon>
                    <span>首页</span>
                </el-menu-item>
                <el-menu-item index="/profile">
                    <el-icon>
                        <User />
                    </el-icon>
                    <span>个人中心</span>
                </el-menu-item>
                <el-menu-item index="/devices">
                    <el-icon>
                        <Monitor />
                    </el-icon>
                    <span>家具管理</span>
                </el-menu-item>
                <el-menu-item index="/tasks">
                    <el-icon>
                        <List />
                    </el-icon>
                    <span>任务管理</span>
                </el-menu-item>
            </el-menu>
        </div>

        <!-- 主内容区 -->
        <div class="main-container">
            <!-- 顶部导航 -->
            <div class="header">
                <div class="breadcrumb">
                    <el-breadcrumb separator="/">
                        <el-breadcrumb-item :to="{ path: '/dashboard' }">首页</el-breadcrumb-item>
                        <el-breadcrumb-item>{{ currentPath }}</el-breadcrumb-item>
                    </el-breadcrumb>
                </div>
                <div class="user-info">
                    <el-dropdown @command="handleCommand">
                        <span class="user-dropdown">
                            {{ username }}
                            <el-icon>
                                <CaretBottom />
                            </el-icon>
                        </span>
                        <template #dropdown>
                            <el-dropdown-menu>
                                <el-dropdown-item command="profile">个人信息</el-dropdown-item>
                                <el-dropdown-item command="password">修改密码</el-dropdown-item>
                                <el-dropdown-item divided command="logout">退出登录</el-dropdown-item>
                            </el-dropdown-menu>
                        </template>
                    </el-dropdown>
                </div>
            </div>

            <!-- 内容区 -->
            <div class="content">
                <router-view></router-view>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useRouter, useRoute } from 'vue-router'
import { House, User, Monitor, List, CaretBottom } from '@element-plus/icons-vue'
import { ElMessageBox } from 'element-plus'

const router = useRouter()
const route = useRoute()

const username = ref(localStorage.getItem('username') || '用户')

const activeMenu = computed(() => route.path)

const currentPath = computed(() => {
    const pathMap: Record<string, string> = {
        '/dashboard': '首页',
        '/profile': '个人中心',
        '/devices': '家具管理',
        '/tasks': '任务管理'
    }
    return pathMap[route.path] || '首页'
})

const handleCommand = async (command: string) => {
    switch (command) {
        case 'profile':
            router.push('/profile')
            break
        case 'password':
            router.push('/change-password')
            break
        case 'logout':
            await ElMessageBox.confirm('确定要退出登录吗？', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            })
            localStorage.removeItem('token')
            localStorage.removeItem('username')
            router.push('/login')
            break
    }
}

const handleSelect = (index: string) => {
    router.push(index)
}
</script>

<style scoped>
.layout-container {
    display: flex;
    height: 100vh;
}

.sidebar {
    width: 200px;
    background-color: #001529;
    color: white;
    display: flex;
    flex-direction: column;
}

.logo {
    height: 60px;
    line-height: 60px;
    text-align: center;
    font-size: 18px;
    font-weight: bold;
    border-bottom: 1px solid #002140;
}

.logo a {
    color: white;
    text-decoration: none;
}

.sidebar-menu {
    border-right: none;
}

.main-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background-color: #f0f2f5;
}

.header {
    height: 60px;
    background-color: white;
    padding: 0 20px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    box-shadow: 0 1px 4px rgba(0, 21, 41, 0.08);
}

.breadcrumb {
    display: flex;
    align-items: center;
}

.user-info {
    display: flex;
    align-items: center;
}

.user-dropdown {
    cursor: pointer;
    display: flex;
    align-items: center;
    gap: 4px;
}

.content {
    flex: 1;
    padding: 20px;
    overflow-y: auto;
}
</style>